﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class moving : MonoBehaviour {

    int vo = 15000;     // 초속도.
    int th = 45;        // 각도.
    float dh = 0;      // 높이.
    float dl = 0;       // 거리.
    float t = 0;         // 시간.
    float w = 0;         // 바람.

    Vector3 originPos;

    public PixelMask _maskTesting;
    
    public GameObject[] _collQuad = new GameObject[8];

//    List<SplitInfo> lstSplitInfo = new List<SplitInfo>();
	
	void Start () {
        originPos = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z);
	}
	
	
	void Update () 
    {
        if (gameObject.activeSelf == false)
            return;

        double r = th * Math.PI / 180;            // 각도를 라디안으로 변환.

        
        dh = (float)(vo * Math.Sin(r) * t - 4.9*100f * t * t);                // vo.sin(th).t - 1/2.G.t**2.
        dl = (float)(vo * Math.Cos(r) * t + w * t * t);                               // 거리.     

        t += Time.deltaTime * 5;

        transform.localPosition = new Vector3(originPos.x + dl / 500, originPos.y + dh / 500, originPos.z);

        //_maskTesting.alphaColorArray

        if (_maskTesting != null)
        {
            //lstSplitInfo.Clear();
            //lstSplitInfo = GameMain.Instance._pixelSplit.ContainPixel(transform.localPosition, gameObject.transform.localScale.x / 2);

//             if (lstSplitInfo.Count > 0)
//             {
//                 for (int i = 0; i < 8; ++i)
//                 {
//                     if (lstSplitInfo.Count > i)
//                     {
//                         SplitInfo splitInfo = lstSplitInfo[i];
//                         _collQuad[i].gameObject.transform.localScale = new Vector3(splitInfo._radius * 2, splitInfo._radius * 2, 1);
//                         _collQuad[i].gameObject.transform.localPosition = new Vector3(splitInfo._center.x, splitInfo._center.y, -1);
//                         _collQuad[i].gameObject.SetActive(true);
//                     }
//                     else
//                     {
//                         _collQuad[i].gameObject.SetActive(false);
//                     }
//                 }
//             }

            Vector2 testCol = Vector2.zero;

            if (WorldmapDataMgr.CollisionPixel(0, transform.localPosition, gameObject.transform.localScale.x / 2, ref testCol))
            {
                int count = 500;
                int x = (int)testCol.x;
                int y = (int)testCol.y;

                for (int j = -(count / 2); j < count / 2; ++j)
                {
                    for (int k = -(count / 2); k < count / 2; ++k)
                    {
                        int realPixelIdx = (x + k) + ((y - j) * 1280);

                        if (realPixelIdx < 0)
                            continue;

                        if( Vector2.Distance(testCol,new Vector2(x+k,y-j)) > 70 )
                            continue;

                        _maskTesting._alphaColorArray[realPixelIdx].a = 0.0f;
                        
                    }
                }

                _maskTesting.UpdatePixel();
                gameObject.SetActive(false);
            }
        }
	}
}
